import {
	_decorator,
	Component,
	Node,
	LabelComponent,
	ButtonComponent,
	EventHandler,
	director,
} from "cc";
const { ccclass, property } = _decorator;

@ccclass("ItemList")
export class ItemList extends Component {

    @property(EventHandler)
    handle:EventHandler=null;

	public index = -1;

	label: LabelComponent = null;

	button: ButtonComponent = null;

	onLoad() {
		this.button = this.node.getComponent(ButtonComponent);
		this.label = this.node.getComponentInChildren(LabelComponent);
		this.index = this.node.getSiblingIndex();
	}

	setEvent() {
        console.log('click');
		let eventHand: EventHandler = new EventHandler();
		eventHand.target = this.node;
		eventHand.component = "ItemList";
		eventHand.handler = "onClick";
		this.button.clickEvents.push(eventHand);
	}

	onClick(e:CustomEvent,data:string) {
        console.log('click');
        
		director.loadScene(this.node.name);
		// this.node.dispatchEvent(new CustomEvent<number>('onClock'))
	}

	// update (deltaTime: number) {
	//     // [4]
	// }

	updateItem(name: string) {
		this.node.name = name;
		this.label.string = name;

		this.setEvent();
	}
}
